package com.example.sxmall.service.action.impl;

import com.example.sxmall.auth.Authentication;
import com.example.sxmall.auth.UserInfo;
import com.example.sxmall.controller.shopCart.GlobalConstant;
import com.example.sxmall.controller.shopCart.vo.ProductVO;
import com.example.sxmall.controller.shopCart.vo.ShopCartVO;
import com.example.sxmall.controller.shopCart.vo.StoreProductVO;
import com.example.sxmall.controller.shopCart.vo.StoreVO;
import com.example.sxmall.redis.ShopCartKey;
import com.example.sxmall.service.ShopCartService;
import com.example.sxmall.service.action.ActionCartService;
import com.example.sxmall.service.dto.OperateCartDTO;
import com.example.sxmall.service.dto.ShopCartDTO;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Description: TODO
 * @Author whh-yt
 * @Date 2022/3/17 4:18 下午
 */
@Service
public class MoreCartActionService extends ActionCartService {

    @Override
    public ShopCartVO execute(OperateCartDTO operateCartDTO) {
        // 需要添加的商品
        Long storeId = operateCartDTO.getStoreId();
        List<ProductVO> productList = operateCartDTO.getProductList();

        UserInfo userInfo = Authentication.getUserInfo();
        ShopCartDTO shopCartCache = redisService.get(ShopCartKey.SHOP_CART_KEY, userInfo.getUnionId(), ShopCartDTO.class);

        ShopCartDTO shopCartDTO = new ShopCartDTO();
        shopCartDTO.setVersion(ShopCartService.SHOPCART_VERSION);
        // 购物车为空
        if (Objects.isNull(shopCartCache)) {
            StoreProductVO storeProductVO = new StoreProductVO();
            storeProductVO.setStore(new StoreVO(GlobalConstant.WAHAHA_STORE_ID, GlobalConstant.WAHAHA_STORE));
            productList.stream().peek(item -> item.setCheck(Boolean.TRUE)).count();
            storeProductVO.setProductList(productList);
            shopCartDTO.setStoreProductList(Collections.singletonList(storeProductVO));
        } else {
            // 版本用来判断
            String version = shopCartCache.getVersion();
            List<StoreProductVO> storeProductListCache = shopCartCache.getStoreProductList();
            // 需要插入的数据
            if (CollectionUtils.isEmpty(storeProductListCache)) {
                StoreProductVO storeProductVO = new StoreProductVO();
                storeProductVO.setStore(new StoreVO(GlobalConstant.WAHAHA_STORE_ID, GlobalConstant.WAHAHA_STORE));
                storeProductVO.setProductList(productList);
                shopCartDTO.setStoreProductList(Collections.singletonList(storeProductVO));
            } else {
                // TODO 目前只有一个店铺 所以简化一下 从购物车取出当前店铺的商品
                StoreProductVO storeProductCache = storeProductListCache.get(0);
                List<ProductVO> productListCache = storeProductCache.getProductList();
                Map<String, ProductVO> cartMapCache = productListCache.stream().collect(Collectors.toMap(ProductVO::getSkuCode, item -> item));
                // 此次添加的商品
                Map<String, ProductVO> skuMap = productList.stream().collect(Collectors.toMap(ProductVO::getSkuCode, item -> item));
                for (Map.Entry<String, ProductVO> skuEntry : skuMap.entrySet()) {
                    String key = skuEntry.getKey();
                    ProductVO value = skuEntry.getValue();
                    if (cartMapCache.containsKey(key)) {
                        ProductVO cartProduct = cartMapCache.get(key);
                        cartProduct.setCheck(Boolean.TRUE);
                        cartProduct.setBuyCount(cartProduct.getBuyCount() + value.getBuyCount());
                    } else {
                        value.setCheck(Boolean.TRUE);
                        productListCache.add(value);
                    }
                }
                storeProductCache.setProductList(productListCache);
                shopCartDTO.setStoreProductList(storeProductListCache);
            }
        }

        redisService.set(ShopCartKey.SHOP_CART_KEY, userInfo.getUnionId(), shopCartDTO);
        return new ShopCartVO();
    }
}
